package uit.com.project.iCampus.services;

import java.util.List;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.webserver.WebServer;
import org.hibernate.Session;

import uit.com.project.iCampus.dao.BuildingInfoDAO;
import uit.com.project.iCampus.dao.DAOFactory;
import uit.com.project.iCampus.dao.UserDAO;
import uit.com.project.iCampus.dao.UserOnlineDAO;
import uit.com.project.iCampus.dao.hibernate.HibernateDAOFactory;
import uit.com.project.iCampus.model.BuildingInfo;
import uit.com.project.iCampus.model.User;
import uit.com.project.iCampus.model.UserOnline;
import uit.com.project.iCampus.util.HibernateUtil;
import uit.com.project.iCampus.util.ListObject;
import uit.com.project.iCampus.util.XMLParserServerUtil;

public class ICampusServicesProvider {
	DAOFactory daoFactory = DAOFactory.instance(HibernateDAOFactory.class);
	UserDAO userDAO = daoFactory.getUserDAO();
	BuildingInfoDAO buildingInfoDAO = daoFactory.getBuildingInfoDAO();
	UserOnlineDAO userOnlineDAO = daoFactory.getUserOnlineDAO();

	Session session = HibernateUtil.getCurrentSession();

	/*-------------------------------User-------------------*/
	public boolean saveNewPassUser(String username, String password) {
		session.getTransaction().begin();
		boolean result = false;
		session.getTransaction().begin();
		User user = userDAO.findById(username, false);
		user.setPassword(password);

		if (user != null) {
			try {
				session.update(user);
				result = true;
			} catch (Exception e) {
				e.printStackTrace();
				result = false;
			}
		}
		session.getTransaction().commit();
		return result;
	}


	public String findUserById(String username) {
		session.getTransaction().begin();
		User user = userDAO.findById(username, true);
		String result = XMLParserServerUtil.marshalObject(user);
		session.getTransaction().commit();
		System.out.println(result);
		return result;
	}


	public boolean createUser(String username, String password, String fullName) {
		session.getTransaction().begin();
		boolean result = false;
		try {
			User user = new User(username, password, fullName);
			session.save(user);
			result = true;
		} catch (Exception e) {
			return false;
		}
		session.getTransaction().commit();
		return result;
	}

	public String checkLogin(String username, String password) {
		session.getTransaction().begin();
		User user = userDAO.checkLogin(username, password);
		String result = XMLParserServerUtil.marshalObject(user);
		session.getTransaction().commit();
		return result;
	}

	public boolean updateUser(String username, String password,
			String fullName) {
		boolean result = false;
		session.getTransaction().begin();
		User user = userDAO.findById(username, false);
		user.setPassword(password);
		user.setFullName(fullName);
		try {
			session.update(user);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
			result = false;
		}
		session.getTransaction().commit();
		return result;
	}
// ---------------------- BuildingInfo -------------------
	/**
	 * Search BuildingInfo with BuildingTypeEnum
	 * @param buildingTypeString
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String searchBuildngInfoWithBuildingTypeEnum(String buildingTypeString){
		session.getTransaction().begin();
		List<BuildingInfo> buildingInfos = buildingInfoDAO.searchBuildngInfoWithBuildingTypeEnum(buildingTypeString);
		ListObject<Object> listObj = new ListObject(buildingInfos);
		String result = XMLParserServerUtil.marshalListObject(listObj);
		session.getTransaction().commit();
		return result;
	}
	/**
	 * get list BuildingInfo in radius
	 * @param botLat
	 * @param topLat
	 * @param leftLon
	 * @param rightLon
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String searchListBuildingInfoInRadius(String botLat, String topLat, String leftLon, String rightLon){
		session.getTransaction().begin();
		List<BuildingInfo> buildingInfos = buildingInfoDAO.getListBuildngInfoInRadius(Float.parseFloat(botLat),
				Float.parseFloat(topLat), Float.parseFloat(leftLon), Float.parseFloat(rightLon));
		ListObject<Object> listObj = new ListObject(buildingInfos);
		String result = XMLParserServerUtil.marshalListObject(listObj);
		session.getTransaction().commit();
		return result;
	}
	//-----------UserOnline--------------
	/**
	 * Get All User Login.
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String getAllUserOnlines(){
		session.getTransaction().begin();
		List<UserOnline> userOnlines = userOnlineDAO.getAllUserOnlines();
		ListObject<Object> listObj = new ListObject(userOnlines);
		String result = XMLParserServerUtil.marshalListObject(listObj);
		session.getTransaction().commit();
		return result;
	}
	
	public void deleteUserOnline(String username){
		session.getTransaction().begin();
		userOnlineDAO.deleteUserOnline(username);
		session.getTransaction().commit();
	}
	
	public static void main(String[] args) {
		try {
			WebServer webServer = new WebServer(8989);
			XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
			PropertyHandlerMapping phm = new PropertyHandlerMapping();
			phm.addHandler("ICampus", ICampusServicesProvider.class);

			xmlRpcServer.setHandlerMapping(phm);
			webServer.start();
			System.out.println("ICampus Server is started!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("ICampus Server can't start!");
		}
	}
}
